package com.dingup.api.Util;

import org.apache.commons.lang.time.DateUtils;

import java.util.*;

/**
 * Created by wancheng on 2018/6/22.
 */
public class CalendarUtil {

    private static final String AM = "am";
    private static final String PM = "pm";

    /**
     * 获取当天 00：00：00
     *
     * @return
     */
    public static Date currentDayZero() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        return zero;
    }

    /**
     * 获取当天 00：00：00
     *
     * @return
     */
    public static Date dayZero(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        return zero;
    }

    /**
     * 获取当天 00：00：00
     *
     * @return
     */
    public static Date dayLast(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date last = calendar.getTime();
        return last;
    }

    /**
     * 小时加减
     *
     * @param date
     * @param value
     * @return
     */
    public static Date getTimeBeforHour(Date date, Integer value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, value);
        Date d = calendar.getTime();
        return d;
    }

    /**
     * 天数加减
     *
     * @param date
     * @param value
     * @return
     */
    public static Date getTimeBeforDay(Date date, Integer value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, value);
        Date d = calendar.getTime();
        return d;
    }

    public static Date getTimeBeforSecond(Date date, Integer value) {
        return getDate(date, value, Calendar.SECOND);
    }

    public static Date getTimeBeforMinute(Date date, Integer value) {
        return getDate(date, value, Calendar.MINUTE);
    }

    private static Date getDate(Date date, Integer value, int type) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(type, value);
        Date d = calendar.getTime();
        return d;
    }

    /**
     * 获取日期所在星期的 周几
     *
     * @param date
     * @param value
     * @return
     */
    public static Date getWeekDay(Date date, Integer value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.DAY_OF_WEEK, value);
        return calendar.getTime();
    }

    public static List<Date> getBetween(Date before, Date after) {
        Date beforeTime = DateUtils.truncate(before, Calendar.DAY_OF_MONTH);
        Date endTime = DateUtils.truncate(after, Calendar.DAY_OF_MONTH);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(beforeTime);
        List<Date> res = new ArrayList<>();
        while (calendar.getTime().getTime() <= endTime.getTime()) {
            res.add(calendar.getTime());
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return res;
    }

    /**
     * 得到本周周一
     *
     * @return yyyy-MM-dd
     */
    public static Date getMondayOfThisWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }
        c.add(Calendar.DATE, -day_of_week + 1);
        return c.getTime();
    }

    /**
     * 得到本周周日
     *
     * @return yyyy-MM-dd
     */
    public static Date getSundayOfThisWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }
        c.add(Calendar.DATE, -day_of_week + 7);
        return c.getTime();
    }

    public static String getAmOrPm(Date date) {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(date);
        if (gregorianCalendar.get(GregorianCalendar.AM_PM) == GregorianCalendar.AM) {
            return AM;
        } else if (gregorianCalendar.get(GregorianCalendar.AM_PM) == GregorianCalendar.PM) {
            return PM;
        } else {
            return null;
        }
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    public static void main(String[] a) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        System.out.print(calendar.AM);
    }
}
